package com.noodles.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.noodles.mall.entity.Item;
import com.noodles.mall.entity.ItemType;
import com.noodles.mall.mapper.ItemMapper;
import com.noodles.mall.service.ItemService;
import com.noodles.mall.service.ItemTypeService;
import com.noodles.mall.vo.PageVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/** 商品 **/
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    private static final Logger logger = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private ItemTypeService itemTypeService;

    /** 增加 **/
    public boolean insert(Item item){
        return super.save(item);
    }

    /** 删除: 商品下架 **/
    public boolean delete(List<Integer> ids){
        return super.update(new UpdateWrapper<Item>().lambda()
                .set(Item::getStatus,0)
                .in(Item::getId,ids)
        );
    }

    /** 修改: **/
    public boolean update(Item item){
        return super.updateById(item);
    }

    /** 修改: 商品id, 图片相对路径 **/
    public boolean updateUrl(Integer id, String url){
        return super.update(new UpdateWrapper<Item>().lambda()
                .set(Item::getImageUrl,url)
                .eq(Item::getId,id)
        );
    }

    /** 查询: 单个 **/
    public Item selectOne(Integer id){
        return super.getById(id).initItemType();
    }

    /** 分页查询: 查询条件wrapper **/
    public PageVo selectPage(Long current, Long size, LambdaQueryWrapper<Item> wrapper){
        Page<Item> page =super.page(new Page<>(current, size), wrapper.orderByDesc(Item::getCreatedTime));
        page.getRecords().forEach(item -> item.initItemType()); // 注入类别信息
        return PageVo.getPageVo(page);
    }

    /** 分页查询: 无 **/
    public PageVo selectPage(Long current, Long size){
        return selectPage(current,size,new QueryWrapper<Item>().lambda());
    }

    /** 分页查询: word(name,title,description) **/
    public PageVo selectPage(Long current, Long size, String word){
        if(itemTypeService.STRING_ITEM_TYPE_MAP.containsKey(word)){
            int typeId=itemTypeService.STRING_ITEM_TYPE_MAP.get(word).getId();
            return selectPage(current,size,typeId);
        }
        return selectPage(current,size,new QueryWrapper<Item>().lambda()
                .like(Item::getName,word).or()
                .like(Item::getTitle,word).or()
                .like(Item::getDescription,word));
    }

    /** 分页查询: 类别id **/
    public PageVo selectPage(Long current, Long size, Integer typeId){
        return selectPage(current,size,new QueryWrapper<Item>().lambda()
                .eq(Item::getTypeId, typeId));
    }

    /** 查询: 商品总销量 **/
    public Object selectSaleCount(){
        List<Object> list=super.listObjs(new QueryWrapper<Item>()
                .select("sum(sale_count)"));
        return list!=null&&list.size()==1 ? list.get(0) : 0;
    }

    /** 列表查询: 根据类别查询销量列表 **/
    public List<Map<String,Object>> selectSaleCountWithType(){
        // 分组统计销量
        List<Map<String,Object>> list=super.listMaps(new QueryWrapper<Item>()
                .select(" type_id as col,count(*) as total").lambda()
                .isNotNull(Item::getTypeId)
                .groupBy(Item::getTypeId)
        );
        // 将typeId替换成对应的typeName
        list.forEach(map->{
            ItemType itemType=itemTypeService.INTEGER_ITEM_TYPE_MAP.get(map.get("col"));
            map.replace("col",itemType.getName());
        });
        return list;
    }
}
